package com.ztools.wx.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ztools.common.util.common.Result;
import com.ztools.wx.config.WxSysConstants;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: seeTools
 * @Date: 2023/4/8  20:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/wx/menu")
public class WxMenuController {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Resource
    private WxMpService wxMpService;

    @PostMapping("/create/{appid}")
    public Result menuCreate(@PathVariable("appid") String appid) {
        // 判断是否有权限创建菜单
        if (StrUtil.isEmpty(appid) || !WxSysConstants.WX_APP_ID.equals(appid)) {
            return Result.error("您无权创建菜单！");
        }
        String version = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        // 组装菜单
        WxMenu menu = new WxMenu();
        // 1.个人信息
        WxMenuButton button1 = new WxMenuButton();
        button1.setType(WxConsts.MenuButtonType.VIEW);
        button1.setName("个人信息");
        // 2.快捷资讯
        WxMenuButton button2 = new WxMenuButton();
        button2.setType(WxConsts.MenuButtonType.VIEW);
        button2.setName("快捷资讯");
        // 3.个人中心
        WxMenuButton button3 = new WxMenuButton();
        button3.setName("个人中心");

        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        // 1.1 个人信息-学生码
        WxMenuButton button1_1 = new WxMenuButton();
        button1_1.setType(WxConsts.MenuButtonType.VIEW);
        button1_1.setName("学生码");
        button1_1.setUrl(WxSysConstants.WX_SERVER_URL + "/pages/member/code.html?v=" + version);
        button1.getSubButtons().add(button1_1);
        // 1.2 个人信息-个人信息
        WxMenuButton button1_2 = new WxMenuButton();
        button1_2.setType(WxConsts.MenuButtonType.VIEW);
        button1_2.setName("个人信息");
        button1_2.setUrl(WxSysConstants.WX_SERVER_URL + "/pages/member/information.html?v=" + version);
        button1.getSubButtons().add(button1_2);

        // 2.1 快捷资讯-美丽校园
        WxMenuButton button2_1 = new WxMenuButton();
        button2_1.setType(WxConsts.MenuButtonType.VIEW);
        button2_1.setName("美丽校园");
        button2_1.setUrl(WxSysConstants.WX_SERVER_URL + "/pages/news/school.html?v=" + version);
        button2.getSubButtons().add(button2_1);

        // 2.2 快捷资讯-美丽校园
        WxMenuButton button2_2 = new WxMenuButton();
        button2_2.setType(WxConsts.MenuButtonType.VIEW);
        button2_2.setName("每日一笑");
        button2_2.setUrl(WxSysConstants.WX_SERVER_URL + "/pages/news/jokes.html?v=" + version);
        button2.getSubButtons().add(button2_2);

        // 3.1 个人中心-个人中心
        WxMenuButton button3_1 = new WxMenuButton();
        button3_1.setType(WxConsts.MenuButtonType.VIEW);
        button3_1.setName("个人中心");
        button3_1.setUrl(WxSysConstants.WX_SERVER_URL + "/pages/member/center.html?v=" + version);
        button3.getSubButtons().add(button3_1);

        // 创建菜单
        try {
            wxMpService.getMenuService().menuCreate(menu);
            return Result.success("菜单创建成功").data(menu);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return Result.error("菜单创建失败");
    }

    @GetMapping("/getMenu")
    public Result getMenu(String appid) {
        // 判断是否有权限创建菜单
        if (StrUtil.isEmpty(appid) || !WxSysConstants.WX_APP_ID.equals(appid)) {
            return Result.error("您无权获取菜单！");
        }
        try {
            WxMpMenu wxMpMenu = wxMpService.getMenuService().menuGet();
            return Result.success().data(wxMpMenu.getMenu());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return null;
    }
}
